﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：回灌表顺序调整
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using System.Text;

namespace Web.Pages.YongShui
{
    public partial class HuiGuanBSXTZ : BasePage
    {
        #region 属性
        
        /// <summary>
        /// 记录当前操作的水表号
        /// </summary>
        private string SHUIBIAOBH
        {
            get
            {
                return Convert.ToString(ViewState["SHUIBIAOBH"]);
            }
            set
            {
                ViewState["SHUIBIAOBH"] = value;
            }
        }
        
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 水表信息对象
        /// </summary>
        YS_CB_SHUIBIAOXX _YS_CB_SHUIBIAOXX = new YS_CB_SHUIBIAOXX();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
                LoadData();
            }
        }

        /// <summary>
        /// 左侧列表焦点变更事件
        /// </summary>
        protected void gv_FocusedRowChanged(object sender, EventArgs e)
        {
            if (this.gvList.FocusedRowIndex == -1)
            {
                return;
            }
            this.SHUIBIAOBH = Convert.ToString(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "VALUE"));            
        }

        /// <summary>
        /// 上移操作
        /// </summary>
        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.SHUIBIAOBH))
            {
                //获取选中项的位置
                int index = -1;
                for (int i = 0; i < this.dtSource.Rows.Count; i++)
                {
                    if (Convert.ToString(dtSource.Rows[i]["VALUE"]) == this.SHUIBIAOBH)
                    {
                        index = i;
                        break;
                    }
                }
                if (index > 0)
                {
                    //先复制数据,移出
                    DataRow row = this.dtSource.NewRow();
                    row.ItemArray = new object[] { this.dtSource.Rows[index][0], this.dtSource.Rows[index][1], this.dtSource.Rows[index][2] };
                    this.dtSource.Rows.RemoveAt(index);
                    //再前移一位插入
                    index = index - 1;
                    this.dtSource.Rows.InsertAt(row, index);
                    //重新绑定
                    this.gvList.DataSource = this.dtSource;
                    this.gvList.DataBind();
                    //选中当前操作项
                    PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "index", index.ToString(), true);
                    this.upList.Update();
                }
            }
        }

        /// <summary>
        /// 下移操作
        /// </summary>
        protected void btnDown_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.SHUIBIAOBH))
            {
                //获取选中项的位置
                int index = -1;
                for (int i = 0; i < this.dtSource.Rows.Count; i++)
                {
                    if (Convert.ToString(dtSource.Rows[i]["VALUE"]) == this.SHUIBIAOBH)
                    {
                        index = i;
                        break;
                    }
                }
                if (index < this.dtSource.Rows.Count - 1)
                {
                    //先复制数据,移出
                    DataRow row = this.dtSource.NewRow();
                    row.ItemArray = new object[] { this.dtSource.Rows[index][0], this.dtSource.Rows[index][1], this.dtSource.Rows[index][2] };
                    //DataRow row = this.dtSource.Rows[index];
                    this.dtSource.Rows.RemoveAt(index);
                    //再前移一位插入
                    index = index + 1;
                    this.dtSource.Rows.InsertAt(row, index);
                    //重新绑定
                    this.gvList.DataSource = this.dtSource;
                    this.gvList.DataBind();
                    //选中当前操作项
                    PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "index", index.ToString(), true);
                    this.upList.Update();
                }
            }
        }

        /// <summary>
        /// 保存当前顺序
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData();
        }

        /// <summary>
        /// 放弃操作
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.LoadData();
            this.upList.Update();
        }

        /// <summary>
        /// 加载数据列表
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            PageTools.LoadCombox(this.cmbZhanDian, PageTools.eLoadType.站点, BusinessTool.ZHANDIANBH(), null, null);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
         
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //查询选择站点下的回灌表
            this.dtSource = BusinessTool.GetTextAndValueAndSelectColumns("YS_CB_SHUIBIAOXX", "S_DIZHI", "S_SHUIBIAOBH", "ID", "-", string.Format("S_ZHANDIANBH='{0}' and I_LEIXING=1 and I_JLZT=0 and I_TYPE>1", this.cmbZhanDian.Value),"I_NO asc").Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }
       
        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            //拼接要更新的记录ID及要更新的序号值
            StringBuilder ids = new StringBuilder("");
            StringBuilder inos = new StringBuilder("");
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                ids.Append(",").Append(this.dtSource.Rows[i]["ID"]);
                inos.Append(",").Append(i);
            }
            if(ids.ToString()!="")
            {
                ids.Append(",");
                inos.Append(",");
                //保存新顺序
                _spHelper.P_UPDATESHUIBIAOINO(ids.ToString(), inos.ToString());
                PageTools.AlertMsg(this,"保存成功！");
            }
        }

        #endregion      
    }
}